package com.lenovo.smarttraffic.wjy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class wjy_logSearch extends BaseActivity {

    private ListView loglist;

    List<Logdata>logdata = new ArrayList<>();
    Myadapter myadapter = new Myadapter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initToolBar(findViewById(R.id.toolbar),true,"日志查询");
        loglist = (ListView) findViewById(R.id.loglist);

        initdata();
        loglist.setAdapter(myadapter);
    }

    private void initdata() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.110:8088/transportservice/action/").addConverterFactory(GsonConverterFactory.create()).build();
        Interface_sence interface_sence = retrofit.create(Interface_sence.class);
        Call<Bean_sencedata> user1 = interface_sence.getCall(new Bean_User1("user1"));
        user1.enqueue(new Callback<Bean_sencedata>() {
            @Override
            public void onResponse(Call<Bean_sencedata> call, Response<Bean_sencedata> response) {
                if (response.code()==200){
                    int pm25243 = response.body().get_$Pm25145();
                    int co2 = response.body().getCo2();
                    int humidity = response.body().getHumidity();
                    int temperature = response.body().getTemperature();
                    int lightIntensity = response.body().getLightIntensity();
                    for (int i = 0; i <4 ; i++) {
                        logdata.add(new Logdata(co2,humidity,lightIntensity,pm25243,temperature,gettime()));

                    }
                }
            }

            @Override
            public void onFailure(Call<Bean_sencedata> call, Throwable t) {
                Toast.makeText(wjy_logSearch.this,"error",Toast.LENGTH_SHORT).show();

            }
        });
    }
    public String gettime(){
        String time = new SimpleDateFormat("yyyy.MM.dd  HH:mm:ss").format(new Date());
        return time;
    }
    @Override
    protected int getLayout() {
        return R.layout.activity_wjy_log_search;
    }
    public class Myadapter extends BaseAdapter{

        @Override
        public int getCount() {
            return logdata.size();
        }

        @Override
        public Object getItem(int i) {
            return logdata.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        private TextView co2text;
        private TextView humitext;
        private TextView pmtext;
        private TextView lighttext;
        private TextView timetext;
        private TextView temptext;


        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.wjy_log_item,viewGroup,false);

            co2text = (TextView) view.findViewById(R.id.co2text);
            humitext = (TextView)view. findViewById(R.id.humitext);
            pmtext = (TextView) view.findViewById(R.id.pmtext);
            lighttext = (TextView) view.findViewById(R.id.lighttext);
            timetext = (TextView) view.findViewById(R.id.timetext);
            temptext = (TextView) view.findViewById(R.id.temptext);
            co2text.setText(logdata.get(i).getCo2()+"");
            humitext.setText(logdata.get(i).getHumi()+"");
            pmtext.setText(logdata.get(i).getPm()+"");
            lighttext.setText(logdata.get(i).getLight()+"");
            timetext.setText(logdata.get(i).getTime()+"");
            temptext.setText(logdata.get(i).getTemp()+"'");
            return view;
        }
    }
    public class Logdata{
        int co2;
        int humi;
        int light;
        int pm;
        int temp;
        String time;

        public int getCo2() {
            return co2;
        }

        public void setCo2(int co2) {
            this.co2 = co2;
        }

        public int getHumi() {
            return humi;
        }

        public void setHumi(int humi) {
            this.humi = humi;
        }

        public int getLight() {
            return light;
        }

        public void setLight(int light) {
            this.light = light;
        }

        public int getPm() {
            return pm;
        }

        public void setPm(int pm) {
            this.pm = pm;
        }

        public int getTemp() {
            return temp;
        }

        public void setTemp(int temp) {
            this.temp = temp;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public Logdata(int co2, int humi, int light, int pm, int temp, String time) {
            this.co2 = co2;
            this.humi = humi;
            this.light = light;
            this.pm = pm;
            this.temp = temp;
            this.time = time;
        }
    }
}
